using System;
using UnityEngine;
using UnityEngine.UI;

public class QuickFirePlayer : MonoBehaviour, IPlayer
{
	[Serializable]
	public class PlayerData : IPlayerData
	{
		public int ShotCount;

		public int PottedCount;

		public float HitRate;

		public int combo;

		public float PlayTime;

		public int AverageTime;

		public int Score;

		public int HighScore;

		public int MaxRank = 1;
	}

	public PlayerData m_PlayerData;

	protected Text m_Multiplier;

	protected Text m_Rack;

	protected Text m_Time;

	protected Text m_Score;

	protected float m_MultiplierValue;

	protected float m_TimeValue;

	protected float m_TimeShowing;

	[SerializeField]
	protected int m_TimeTurningSpeed = 60;

	protected TimeSpan m_TimeSpan;

	protected bool m_PottedBallThisRound;

	protected bool m_MarkPotted;

	[SerializeField]
	protected Image[] m_BallIcons;

	public int playerID
	{
		get;
		set;
	}

	public float PlayTime
	{
		get
		{
			return m_PlayerData.PlayTime;
		}
		set
		{
			m_PlayerData.PlayTime = value;
		}
	}

	public int Rank
	{
		get
		{
			return m_PlayerData.MaxRank;
		}
		set
		{
			m_PlayerData.MaxRank = value;
			m_Rack.text = value.ToString();
		}
	}

	private void Awake()
	{
		m_Multiplier = base.transform.Find("Multiplier").Find("Value").GetComponent<Text>();
		m_Rack = base.transform.Find("Rack").Find("Value").GetComponent<Text>();
		m_Time = base.transform.Find("Time").Find("Value").GetComponent<Text>();
		m_Score = base.transform.Find("Score").Find("Value").GetComponent<Text>();
		PoolRulesBase.onGameOver = (Delegate1Args<IPlayer>)Delegate.Combine(PoolRulesBase.onGameOver, new Delegate1Args<IPlayer>(OnGameOver));
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Combine(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		PoolRulesBase.onCueballPotted = (Delegate0Args)Delegate.Combine(PoolRulesBase.onCueballPotted, new Delegate0Args(ComboBreak));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Combine(PoolRulesBase.onNewTurn, new Delegate1Args<int>(TurnBegin));
		m_MultiplierValue = 1f;
		m_PlayerData = new PlayerData();
		m_PlayerData.HighScore = ConstantData.quickFireRecords.HighScore;
	}

	private void OnDestroy()
	{
		PoolRulesBase.onGameOver = (Delegate1Args<IPlayer>)Delegate.Remove(PoolRulesBase.onGameOver, new Delegate1Args<IPlayer>(OnGameOver));
		PoolRulesBase.onFireBall = (Delegate0Args)Delegate.Remove(PoolRulesBase.onFireBall, new Delegate0Args(OnFireBall));
		PoolRulesBase.onCueballPotted = (Delegate0Args)Delegate.Remove(PoolRulesBase.onCueballPotted, new Delegate0Args(ComboBreak));
		PoolRulesBase.onNewTurn = (Delegate1Args<int>)Delegate.Remove(PoolRulesBase.onNewTurn, new Delegate1Args<int>(TurnBegin));
	}

	private void Update()
	{
		if (m_TimeShowing == m_TimeValue)
		{
			return;
		}
		if (m_TimeShowing < m_TimeValue)
		{
			m_TimeShowing += Time.deltaTime * (float)m_TimeTurningSpeed;
			if (m_TimeShowing > m_TimeValue)
			{
				m_TimeShowing = m_TimeValue;
			}
		}
		else if (m_TimeShowing > m_TimeValue)
		{
			m_TimeShowing -= Time.deltaTime * (float)m_TimeTurningSpeed;
			if (m_TimeShowing < m_TimeValue)
			{
				m_TimeShowing = m_TimeValue;
			}
		}
		m_TimeSpan = TimeSpan.FromSeconds(m_TimeShowing);
		string str = (m_TimeSpan.Minutes >= 10) ? m_TimeSpan.Minutes.ToString() : ("0" + m_TimeSpan.Minutes);
		string str2 = (m_TimeSpan.Seconds >= 10) ? m_TimeSpan.Seconds.ToString() : ("0" + m_TimeSpan.Seconds);
		m_Time.text = str + ":" + str2;
	}

	protected void OnLoadedPlayerDataOnAndroid(PlayerData data)
	{
		if (data != null)
		{
			m_PlayerData.HighScore = data.HighScore;
		}
	}

	private bool OpenRateAppDialog()
	{
		/* if (!PlayerPrefs.HasKey("StarRateKey"))
		{
			UIADViewWnd.Show(UIADViewWnd.ADViewType.Rate);
			return true;
		} */
		return false;
	}

	protected void OnGameOver(IPlayer player)
	{
		Action action = delegate
		{
			m_PlayerData.HitRate = ((m_PlayerData.ShotCount != 0) ? ((float)m_PlayerData.PottedCount / (float)m_PlayerData.ShotCount) : 0f);
			if (m_PlayerData.HighScore < m_PlayerData.Score)
			{
				m_PlayerData.HighScore = m_PlayerData.Score;
				ConstantData.quickFireRecords = m_PlayerData;
			}
			GameStatistics.MarkQuickFireHighScore(m_PlayerData.Score);
			if (ConstantData.vedioQuickFireCount > 0 && AdManagerOld.instance.curAd.HasVideo() && !ConstantData.watchedAdAtThisBattle)
			{
				TextDialog.Show2Button(HOLocalizationConfiguration.GetValue(337), OnDialogSure, OnDialogCancel);
				ConstantData.vedioQuickFireCount--;
			}
			else
			{
				BaseUIController.MSettlement.GameOver(m_PlayerData);
			}
		};
		if (OpenRateAppDialog())
		{
			UIADViewWnd.OnRateThisAppDestroy = action;
		}
		else
		{
			action();
		}
	}

	protected void OnDialogSure()
	{
		ConstantData.watchedAdAtThisBattle = true;
		AdManagerOld.instance.PlayVideo(3, delegate
		{
		});
	}

	protected void OnDialogCancel()
	{
		BaseUIController.MSettlement.GameOver(m_PlayerData);
	}

	public void SetTime(float time, bool immediate = false)
	{
		m_TimeValue = ((!(time > 0f)) ? 0f : time);
		if (immediate)
		{
			m_TimeShowing = m_TimeValue;
		}
	}

	public void AddScore(float score)
	{
		m_PlayerData.Score += (int)(score * m_MultiplierValue);
		m_Score.text = m_PlayerData.Score.ToString();
	}

	public void AddBall(int ballID)
	{
		int i = 0;
		for (int num = m_BallIcons.Length; i < num; i++)
		{
			if (m_BallIcons[i].sprite == null)
			{
				m_BallIcons[i].sprite = Pools.BallIcons[ballID - 1];
				if (i == num - 1)
				{
					iTween.FadeTo(m_BallIcons[i].gameObject, iTween.Hash("alpha", 1, "time", 0.5f, "oncomplete", "Combo", "oncompletetarget", base.gameObject));
				}
				else
				{
					iTween.FadeTo(m_BallIcons[i].gameObject, 1f, 0.5f);
				}
				break;
			}
		}
		if (!m_MarkPotted)
		{
			m_PlayerData.PottedCount++;
			m_MarkPotted = true;
		}
		m_PlayerData.combo++;
		GameStatistics.MarkMaxCombo(m_PlayerData.combo);
	}

	protected void Combo()
	{
		m_MultiplierValue += 0.5f;
		m_Multiplier.text = m_MultiplierValue.ToString() + "x";
		BallIconFadeoff();
	}

	public void ComboBreak()
	{
		m_MultiplierValue -= 0.5f;
		if (m_MultiplierValue < 1f)
		{
			m_MultiplierValue = 1f;
		}
		m_Multiplier.text = m_MultiplierValue.ToString() + "x";
		BallIconFadeoff();
	}

	public void BallIconFadeoff()
	{
		int i = 0;
		for (int num = m_BallIcons.Length; i < num - 1; i++)
		{
			iTween.FadeTo(m_BallIcons[i].gameObject, 0f, 0.5f);
		}
		iTween.FadeTo(m_BallIcons[m_BallIcons.Length - 1].gameObject, iTween.Hash("alpha", 0, "time", 0.5f, "oncomplete", "ClearBallIcons", "oncompletetarget", base.gameObject));
	}

	protected void TurnBegin(int turn)
	{
		m_MarkPotted = false;
	}

	protected void ClearBallIcons()
	{
		int i = 0;
		for (int num = m_BallIcons.Length; i < num; i++)
		{
			m_BallIcons[i].sprite = null;
		}
	}

	protected void OnFireBall()
	{
		m_PlayerData.ShotCount++;
	}
}
